﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Security;
namespace John
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";

        }
        private bool CheckUser(string userName, string strPassword)//检查 管理员名和密码是否正确
        {
            bool bTemp = false;

            strPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(strPassword, "MD5");

            DBConn myDB = new DBConn();
            string mySql = "select hyname,pws from Muser where hyname='" + userName + "' and pws='" + strPassword + "'";
            SqlDataReader mydr = myDB.getDataReader(mySql);
            if (mydr.Read())
            {
                bTemp = true;
            }
            else
            {
                bTemp = false;
            }

            mydr.Close();
            myDB.Close();

            return bTemp;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = CleanString.htmlInputText(username.Value.Trim());
            string strAdminPW = CleanString.htmlInputText(pws.Value.Trim());

            ////测试：删除Cookie
            //HttpCookie cookie = Request.Cookies["name"];
            //if (cookie != null)
            //{
            //    cookie.Expires = DateTime.Today.AddDays(-1);
            //    Response.Cookies.Add(cookie);
            //}

            ////TODO:跳转到登陆前的页面

            //// 记录Cookie
            //FormsAuthentication.SetAuthCookie(name, false);

            //String redirectionUrl = FormsAuthentication.GetRedirectUrl(name, false);
            //if (redirectionUrl.ToLower().IndexOf("login.aspx") > 0)
            //{
            //    redirectionUrl = "Frame.htm";
            //}
            //Response.Redirect(redirectionUrl);



            if (CheckUser(name, strAdminPW))
            {
                //HttpCookie myCookie = new HttpCookie("name", name);//写入Cookies
                //HttpCookie myCookie = new HttpCookie("name", name);//先用jisuanji1110测试，以后用上面替代
                //Response.Cookies.Add(myCookie);
                //Response.Redirect("~/viste/default.aspx?name=" + name);

                Session["name"] = name;
                Response.Redirect("~/viste/default.aspx?name=" + name);

            }
            else
            {
                Response.Write("<script>alert('用户名/密码不正确!!!');</script>");
                Response.Write("<script>history.go(-1);</script>");
            }

        }
    }
}
